/*
 *    Copyright (C) 2011-2012 Hai Bison
 *
 *    See the file LICENSE at the root directory of this project for copying
 *    permission.
 */

/*
 * PanelEmailMessage.java
 *
 * Created on Sep 14, 2011, 3:29:54 PM
 */
package group.pals.mailgate.ui;

import group.pals.mailgate.beans.ItemEmailMessage;
import group.pals.mailgate.threads.EmailReader;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.MatteBorder;
import org.apache.commons.lang3.StringEscapeUtils;

/**
 *
 * @author Hai Bison
 */
public class PanelEmailMessage extends javax.swing.JPanel {

    private final group.pals.mailgate.utils.Utils _Utils;

    /** Creates new form PanelEmailMessage */
    public PanelEmailMessage() {
        this._Utils = group.pals.mailgate.utils.Utils.getInstance();
        initComponents();
    }
    private boolean newMessage = false;

    public void setEmailMessageInfo(ItemEmailMessage emailMessage, EmailReader emailReader,
            Component parent, int paddingWidth) {
        newMessage = emailMessage.isNewMessage();

        //date
        Calendar calEmailMessage = Calendar.getInstance();
        calEmailMessage.setTime(emailMessage.getReceivedDate());
        SimpleDateFormat sdf = null;
        if (isToday(emailMessage.getReceivedDate())) {
            sdf = new SimpleDateFormat("HH:mm:ss");
        } else {
            Calendar calToday = Calendar.getInstance();
            calToday.setTime(new Date());
            if (calEmailMessage.get(Calendar.YEAR) == calToday.get(Calendar.YEAR)) {
                sdf = new SimpleDateFormat("EEE, dd MMM");
            } else {
                sdf = new SimpleDateFormat("yyyy.MM.dd");
            }
        }
        labelReceivedDate.setText(sdf.format(emailMessage.getReceivedDate()));
        labelReceivedDate.setIcon(emailMessage.hasAttachment() ? _Utils.getIconAttachment24xy() : null);

        //sender
        labelSender.setText(emailMessage.getSender());
        labelSender.setToolTipText(labelSender.getText());
        int width = parent.getWidth()
                //            - (int) (labelReceivedDate.getFontMetrics(labelReceivedDate.getFont()).stringWidth(labelReceivedDate.getText()) * 1.5)
                - labelReceivedDate.getPreferredSize().width
                //TODO: hardcode
                - labelReceivedDate.getFontMetrics(labelReceivedDate.getFont()).stringWidth("date")
                - paddingWidth;
        if (emailMessage.hasAttachment()) {
            width -= _Utils.getIconAttachment24xy().getIconWidth();
        }
        labelSender.setPreferredSize(new Dimension(width,
                (int) (labelSender.getFont().getSize() * 1.5)));

        //subject
        labelSubject.setText(String.format("<html><p><div width=\"%d\"><b>%s</b></div></p><div height=\"10\"></div></html>",
                parent.getWidth() - paddingWidth, StringEscapeUtils.escapeHtml4(emailMessage.getSubject())));

        updateUI();
    }

    private boolean isToday(Date date) {
        Calendar calToday = Calendar.getInstance();
        calToday.setTime(new Date());
        Calendar calOther = Calendar.getInstance();
        calOther.setTime(date);
        return calToday.get(Calendar.YEAR) == calOther.get(Calendar.YEAR)
                && calToday.get(Calendar.MONTH) == calOther.get(Calendar.MONTH)
                && calToday.get(Calendar.DATE) == calOther.get(Calendar.DATE);
    }

    @Override
    public void updateUI() {
        super.updateUI();

        boolean isCrossPlatformLookAndFeel = true;
        if (UIManager.getLookAndFeel() != null) {
            isCrossPlatformLookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName().equals(UIManager.getLookAndFeel().getClass().getName());
        }
        if (isCrossPlatformLookAndFeel) {
//      if (labelSender != null)
//        labelSender.setForeground(Color.red);
            if (labelReceivedDate != null) {
                labelReceivedDate.setForeground(Color.darkGray);
            }
            if (labelSubject != null) {
                labelSubject.setForeground(newMessage ? Color.red : Color.blue);
            }
        }
    }

    public void setSelected(boolean value) {
        if (value) {
            final int Width = 2;
            setBorder(new MatteBorder(new Insets(Width, Width, Width, Width), Color.darkGray));
        } else {
            setBorder(new EtchedBorder());
        }
    }

    public boolean isSelected() {
        return getBorder() instanceof MatteBorder;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        labelSender = new javax.swing.JLabel();
        labelReceivedDate = new javax.swing.JLabel();
        labelSubject = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        labelSender.setFont(new java.awt.Font("Dialog", 0, 12));
        labelSender.setText("labelSender");
        labelSender.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(8, 8, 0, 8);
        add(labelSender, gridBagConstraints);

        labelReceivedDate.setFont(new java.awt.Font("Dialog", 3, 12)); // NOI18N
        labelReceivedDate.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        labelReceivedDate.setText("labelReceivedDate");
        labelReceivedDate.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_END;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(8, 8, 0, 8);
        add(labelReceivedDate, gridBagConstraints);

        labelSubject.setFont(new java.awt.Font("Dialog", 0, 12));
        labelSubject.setText("labelSubject");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.ABOVE_BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 0, 8);
        add(labelSubject, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel labelReceivedDate;
    private javax.swing.JLabel labelSender;
    private javax.swing.JLabel labelSubject;
    // End of variables declaration//GEN-END:variables
}
